var app = getApp();
Page({
    data: {
        sectionData: [],
        params: {
            filterArr: []
        },
        tempArray: [],
        tempSubArray: [],
        goods_price: {
            "衬衣": "398、598、798、1198",
            "西装": "1180、1280、3080、3880、4880、5880",
            "大衣": "3300、4600、7300",
            "配饰": "128、248、268、298、788"
        }
    },
    onLoad: function (options) {
        var goods_type = options.goods_type;
        this.setData({
            goods_type: goods_type
        })
    },
    onReady: function () {
        this.init();
    },//初始化页面
    init: function () {
        var that = this;
        var goods_type = that.data.goods_type;
        wx.request({
            url: app.config() + 'index/getFilters',
            data: {
                "type": goods_type
            },
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                var data = res.data.data;
                var arrKey = [];
                for (var i = 0; i < data.length; i++) {
                    var elements = data[i].content.split("、");
                    res.data.data[i].content = [];
                    for (var key in that.data.goods_price) {
                        if (data[i]['goods_label'] == "sales_money") {
                            var goods_price = that.data.goods_price;
                            var elements = goods_price[that.data.goods_type].split("、");
                        }
                    }
                    for (var j = 0; j < elements.length; j++) {
                        var e = {
                            "key": data[i]['goods_label'],
                            "value": elements[j],
                            "active": false

                        };
                        res.data.data[i].content.push(e);
                    }
                }
                that.setData({
                    sectionData: res.data.data
                })
            }
        })
    },
    taps: function (e) {
        var that = this;
        var name = e.target.dataset.title.split("-");
        var key = name[0];
        var value = name[1];
        var arr = this.data.goods_label;
        var index = e.target.dataset.index.split("-");
        var p_index = index[1];//父索引
        var s_index = index[0];//子索引
        var data = that.data.sectionData;
        // 将获取到的goods_label值拿出来存在一个临时数组里
        if (!this.inArray(this.data.tempArray, key)) {
            that.data.tempArray.push(key)
            var tempArrayIndex = this.data.tempArray.length;
            for (var i = 0; i < tempArrayIndex; i++) {
                var str = "";
                str = {
                    "goods_label": this.data.tempArray[i],
                    "content": {}
                };
            }
            that.data.params.filterArr.push(str);
        }
        var filterArrIndex = this.data.params.filterArr.length
        for (var j = 0; j < filterArrIndex; j++) {
            if (this.data.params.filterArr[j]["goods_label"] == key) {
                if (!this.inArray(that.data.params.filterArr[j].content, value)) {
                    var str = {};
                    str[s_index] = value;
                    that.data.params.filterArr[j].content[s_index] = value;
                }
            }
        }
        var active = data[p_index].content[s_index].active;
        if (!active) {
            data[p_index].content[s_index].active = true;
        } else {
            data[p_index].content[s_index].active = false;
            var filterArrIndex = this.data.params.filterArr.length
            for (var j = 0; j < filterArrIndex; j++) {
                if (this.data.params.filterArr[j]["goods_label"] == key) {
                    if (!this.inArray(that.data.params.filterArr[j].content, value)) {
                        delete that.data.params.filterArr[j].content[s_index];
                    }
                }
            }
        }
        that.setData({
            sectionData: data
        })
    },//判断一个元素是否在数组中
    inArray: function (arr, obj) {
        var i = arr.length;
        while (i--) {
            if (arr[i] === obj) {
                return true;
            }
        }
        return false;
    },//确定
    submit: function () {
        var that = this;
        var data = that.data.params;
        var goods_type = that.data.goods_type;
        wx.setStorage({
            key: 'selectParams',
            data: data
        })
        wx.navigateBack({
            delta: 1
        })
        // var url = '../classify/classify?type=' + goods_type;
        // wx.redirectTo({
        //     url: url
        // })
    },//重置
    reset: function () {
        this.init();
        this.setData({
            params: {
                filterArr: []
            }
        })
    }
})